import BasicTool from "./BasicTool";
import * as turf from '@turf/turf'

export default class TurfTool {

    constructor(viewer) {
        this.viewer = viewer;
        this.basicTool = new BasicTool(this.viewer)
        this._dataSource = new Cesium.CustomDataSource('turf_dataSource');
        this.viewer.dataSources.add(this._dataSource);
    }

    /**
     * 计算两点的距离
     * @param {*} lnglat1 点1的经纬度坐标 格式 [113.2303,23.0234]
     * @param {*} lnglat2 点2的经纬度坐标 格式 [113.2305,23.0434]
     * @param {*} units 单位，可以是 degrees, radians, miles, or kilometers 默认kilometers 千米
     */
    distance(lnglat1, lnglat2, units = 'kilometers') {
        var from = turf.point(lnglat1);
        var to = turf.point(lnglat2);
        var options = {
            units: units
        };

        var distance = turf.distance(from, to, options);
        return distance;
    }

    /**
     * 测量实体点的长度
     * @param points  构造实体的点坐标 格式[[115, -32], [131, -22], [143, -25], [150, -34]]
     * @param {*} units 单位，可以是 degrees, radians, miles, or kilometers 默认kilometers 千米
     */
    geoLength(points, units) {
        var line = turf.lineString(points);
        var option = {units: units}
        var length = turf.length(line, option);
        return length;
    }

    /**
     * 计算两点间的中点
     * @param position1  cartesian对象
     * @param position2  cartesian
     */
    midpointByCartesian(position1, position2) {
        var p1 = this.basicTool.cartesian2LonLat(position1);
        var p2 = this.basicTool.cartesian2LonLat(position2);
        var point1 = turf.point(p1);
        var point2 = turf.point(p2);

        var midpoint = turf.midpoint(point1, point2);
        return midpoint;
    }

    /**
     * 计算两点间的中点
     * @param p1  点坐标 格式 [lng,lat]
     * @param p2  
     */
    midpoint(p1, p2) {
        var point1 = turf.point(p1);
        var point2 = turf.point(p2);

        var midpoint = turf.midpoint(point1, point2);
        return midpoint;
    }

    /**
     * 根据某点生成缓冲区
     * @param lnglat 格式为[-90.548630, 14.616599]
     * @param units 单位  可以是 degrees, radians, miles, or kilometers 默认kilometers 千米
     */
    bufferByLnglat(lnglat,units="kilometers") {
        var point = turf.point(lnglat);
        var option = {
            units: units
        }
        var buffered = turf.buffer(point, 500, option);
        // 转换为cesium实体形状的点集合
        var points = this.reduceDimension(buffered).geometry.coordinates[0]
        return this.viewer.entities.add({
            id: "buffer",
            polygon: {
              hierarchy: Cesium.Cartesian3.fromDegreesArray(points, Cesium.Ellipsoid.WGS84),
              material: Cesium.Color.SKYBLUE.withAlpha(0.5)
            }
        })
    }

    /**
     * 根据某点生成缓冲区
     * @param point cartesian3对象
     * @param dis 生成的缓冲区距离
     * @param units 单位  可以是 degrees, radians, miles, or kilometers 默认kilometers 千米
     */
    buffer(point,dis=1,units="kilometers") {
        var lnglat = this.basicTool.cartesian2ArrayLonLat(point)
        var point = turf.point(lnglat);
        var option = {
            units: units
        }
        var buffered = turf.buffer(point, dis, option);
        // 转换为cesium实体形状的点集合
        var points = this.reduceDimension(buffered.geometry.coordinates[0])
        return this.viewer.entities.add({
            id: "buffer",
            polygon: {
              hierarchy: Cesium.Cartesian3.fromDegreesArray(points, Cesium.Ellipsoid.WGS84),
              material: Cesium.Color.SKYBLUE.withAlpha(0.5)
            }
        })
    }

    /**
     * 计算面积
     *
     * @param {*} positions cartesian3点位对象
     * @return {*}
     */
     computeArea(positions) {
        var arr = [];
        for (var i = 0; i < positions.length; i++) {
            var p = this.cartesian2LonLat(positions[i]);
            arr.push([p.lon, p.lat]);
        }
        arr.push(arr[0]); //终点和起点重合

        var polygon = turf.polygon([arr]);
        var area = turf.area(polygon) / 1000 / 1000;
        return area;
    }

    reduceDimension(arr) {
        var reduced = [];
        for (var i = 0; i < arr.length; i++) {
          reduced = reduced.concat(arr[i]);
        }
        return reduced;
    }
 
    /**
     * 重置所有参数
     * @private
     */
    _resetParams() {
        if (this._dataSource != null) {
            this._dataSource.entities.removeAll();
            this.viewer.dataSources.remove(this._dataSource);
        }
    }
 
    /**
     * 清除
     */
    clearAll() {
        this._resetParams();
    }

}
